<script setup>
import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import request from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

/**
 * 本地存储的账户信息
 *
 * @type {any}
 */
const account = JSON.parse(localStorage.getItem('account-info') || '{}')

/**
 * 房间列表
 * @type {*[]}
 */
let roomOptions = []

/**
 * 企业数据
 *
 * @type {UnwrapNestedRefs<{total: number, form: {businessEmail: string, businessUpdateTime: string, businessSex: string, businessName: string, businessIntroduction: string, businessUpdateMan: string, businessRoomName: string, businessCreateMan: string, businessNo: string, businessAge: number, businessPhone: string, businessCountNumber: string, businessOrderClassHeadName: string, businessRoomNo: string, businessCreateTime: string}, businessName: string, pageSize: number, tableData: *[], pageNum: number}>}
 */
const businessData = reactive({
    businessName: '',
    tableData: [],
    total: 0,
    pageSize: 3,
    pageNum: 1,
    formVisible: false, // 是否显示弹窗
    form: {
        businessNo: '',
        businessName: '',
        businessAge: 0,
        businessSex: '',
        businessIntroduction: '',
        businessOrderClassHeadName: '',
        businessRoomNo: '',
        businessRoomName: '',
        businessPhone: '',
        businessEmail: '',
        businessCreateMan: '',
        businessCreateTime: '',
        businessUpdateMan: '',
        businessUpdateTime: '',
        businessCountNumber: '',
    }
})

/**
 * 加载企业
 */
const loadBusiness = () => {
    request.get(`/api/business/find-business-page`, {
        params: {
            pageNum: businessData.pageNum,
            pageSize: businessData.pageSize,
            businessName: businessData.businessName
        }
    }).then(res => {
        businessData.tableData = res.data?.list || []
        businessData.total = res.data?.list.map(item => item.businessCountNumber)[0] || 0;
    })
}
// 调用企业加载方法，获取后台数据
loadBusiness()

/**
 * 翻页
 */
const handelCurrentChange = () => {
    // 翻页时，重新加载数据即可
    loadBusiness()
}

/**
 * 重置
 */
const reset = () => {
    businessData.businessName = ''
    // 重置时，重新加载数据即可
    loadBusiness()
}

/**
 * 删除企业
 *
 * @param businessNo 企业号
 */
const deleteBusiness = (businessNo) => {
    ElMessageBox.confirm("删除数据后无法恢复，您确认删除此条数据？", "删除确认", {type: 'warning'}).then(() => {
        if (account.accountRoleName === 'visitor-admin') {
            ElMessage.info("游客-管理员仅可以查看信息")
        } else {
            request.delete(`/api/business/delete-business/${businessNo}`).then(res => {
                if (res.code === "200") {
                    // 重新获取数据
                    loadBusiness()
                    ElMessage.success("删除成功")
                } else {
                    ElMessage.error(res.msg)
                }
            })
        }
    }).catch(() => {
        ElMessage({
            type: "info",
            message: "取消删除",
        })
    })
}

/**
 * 加载房间
 */
const loadRoom = () => {
    request.get(`/api/room/find-room`, {}).then(res => {
        roomOptions = res.data
    })
}
// 调用房间方法，获取后台数据
loadRoom()

/**
 * 修改企业
 *
 * @param row 按行修改
 */
const editBusiness = (row) => {
    businessData.form.businessNo = row.businessNo
    businessData.form.businessName = row.businessName
    businessData.form.businessOrderClassHeadName = row.businessOrderClassHeadName
    businessData.form.businessRoomNo = row.businessRoomNo
    businessData.form.businessRoomName = row.businessRoomName
    businessData.formVisible = true
}

/**
 * 提交修改的企业
 */
const submitEditBusiness = () => {
    if (account.accountRoleName === 'visitor-admin') {
        ElMessage.info("游客-管理员仅可以查看信息")
    } else {
        businessData.form.businessUpdateMan = account.accountNo
        request.put(`/api/business/update-business`, businessData.form).then(res => {
            if (res.code === "200") {
                // 重新获取数据
                loadBusiness()
                // 关闭弹窗
                businessData.formVisible = false
                ElMessage.success("编辑成功")
            } else {
                ElMessage.error(res.msg)
            }
        })
    }
}
</script>

<template>
    <div v-if="account.accountRoleName === 'system-admin' || account.accountRoleName === 'visitor-admin'">
        <div class="card" style="margin-bottom: 10px;">
            <el-input style="width: 300px; margin-right: 10px;" v-model="businessData.businessName"
                      placeholder="请输入企业名称查询"
                      :prefix-icon="Search"/>
            <el-button @click="loadBusiness" type="primary" style="margin-left: 10px;">查询</el-button>
            <el-button @click="reset" type="info">重置</el-button>
        </div>
        <div class="card" style="margin-bottom: 10px;">
            <div>
                <el-table :data="businessData.tableData" style="width: 100%">
                    <el-table-column prop="businessNo" label="企业号" width="100px"/>
                    <el-table-column prop="businessName" label="企业名" width="100px"/>
                    <el-table-column prop="businessOrderClassHeadName" label="订单班负责人" width="70px"/>
                    <el-table-column prop="businessAge" label="年龄" width="60px"/>
                    <el-table-column prop="businessSex" label="性别" width="70px"/>
                    <el-table-column prop="businessPhone" label="手机号"/>
                    <el-table-column prop="businessEmail" label="电子邮箱"/>
                    <el-table-column prop="businessRoomName" label="办公室"/>
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button @click="editBusiness(scope.row)" type="primary" size="small" plain>编辑
                            </el-button>
                            <el-button @click="deleteBusiness(scope.row.businessNo)" type="danger" size="small"
                                       plain>删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div class="card">
            <el-pagination v-model:current-page="businessData.pageNum"
                           v-model:page-size="businessData.pageSize"
                           @current-change="handelCurrentChange"
                           background layout="prev, pager, next" :total="businessData.total"/>
        </div>

        <el-dialog v-model="businessData.formVisible" :close-on-click-modal="false" title="企业信息">
            <el-form :model="businessData.form" label-width="100px" label-position="right"
                     style="padding-right: 30px;">
                <el-form-item label="企业号">
                    <el-input placeholder="未填写" v-model="businessData.form.businessNo"
                              autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="企业名">
                    <el-input placeholder="未填写" v-model="businessData.form.businessName"
                              autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="订单班负责人">
                    <el-input placeholder="未填写" v-model="businessData.form.businessOrderClassHeadName"
                              autocomplete="off" disabled></el-input>
                </el-form-item>
                <div class="block" style="margin-bottom: 20px;margin-left: 45px">
                    <el-select
                            v-model="businessData.form.businessRoomNo"
                            class="m-2"
                            placeholder="请选择办公室"
                            style="width: 525px"
                    >
                        <el-option
                                v-for="item in roomOptions"
                                :key="item.roomNo"
                                :label="item.roomName"
                                :value="item.roomNo"
                        />
                    </el-select>
                </div>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="businessData.formVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitEditBusiness">提交</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped>

</style>